package org.excel.web;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.excel.model.Course;
import org.excel.model.InfoVo;
import org.excel.util.ImportExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class CourseController {

    List<Course> documents = Arrays.asList(
            new Course(1, "Spring MVC Xls View", new Date()),
            new Course(2, "Spring MVC Xlsx View", new Date()),
            new Course(3, "Spring MVC XlsxStreaming View", new Date())
    );

    @RequestMapping(value="/excel",method = RequestMethod.GET)
    public String getDocuments(Model model) {
        model.addAttribute("courses", documents);
        return "excel";

    }
    
    @RequestMapping(value="/xls",method = RequestMethod.GET)
    public String getDocumentsxls(Model model) {
        model.addAttribute("courses", documents);
        return "xls";

    }
    
    @RequestMapping(value="/xlsx",method = RequestMethod.GET)
    public String getDocumentsxlsx(Model model) {
        model.addAttribute("courses", documents);
        return "xlsx";

    }
    
    @RequestMapping(value="/stream",method = RequestMethod.GET)
    public String getDocumentsxlsstream(Model model) {
        model.addAttribute("courses", documents);
        return "stream";

    }
    
    @RequestMapping("/excelUpload")
	public String fileUpload(MultipartFile file,HttpServletRequest request) {
    	InputStream in =null;  
        List<List<Object>> listob = null; 
    	
    	try {
			in = file.getInputStream();  
			listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());  
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}  
          
        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出  
        for (int i = 0; i < listob.size(); i++) {  
            List<Object> lo = listob.get(i);  
            InfoVo vo = new InfoVo();  
            vo.setCode(String.valueOf(lo.get(0)));  
            vo.setName(String.valueOf(lo.get(1)));  
            vo.setDate(String.valueOf(lo.get(2)));  
            vo.setMoney(String.valueOf(lo.get(3)));  
              
            System.out.println("打印信息-->机构:"+vo.getCode()+"  名称："+vo.getName()+"   时间："+vo.getDate()+"   资产："+vo.getMoney());  
        }  
		return "list";
	}
}
